package com.xhh.enjoyapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xhh.enjoyapp.Bean.Community;
import com.xhh.enjoyapp.Bean.Post;
import com.xhh.enjoyapp.R;
import com.xhh.enjoyapp.activity.Receive;
import com.xhh.enjoyapp.constant.CommonConstant;

import java.util.List;

/**
 * @description
 * @author: weiXhh
 * @create: 2020-05-14 18:46
 **/
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<Community> data;

    private int maxNum = 15;
    private Boolean isFootView = true; //是否footview

    public ChatAdapter(Context context, List<Community> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_recycle_view_item,parent,false);
        View footView = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_recycle_view_foot_item, parent, false);
        if(viewType == CommonConstant.F_TYPE){
            return new ChatAdapter.RecyclerViewHolder(footView, CommonConstant.F_TYPE);
        } else {
            return new ChatAdapter.RecyclerViewHolder(view, CommonConstant.N_TYPE);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(isFootView && getItemViewType(position) == CommonConstant.F_TYPE){
            final ChatAdapter.RecyclerViewHolder recyclerViewHolder = (ChatAdapter.RecyclerViewHolder) holder;
            recyclerViewHolder.loading.setText("加载中...");
            // 通知
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    maxNum += 8;
                    notifyDataSetChanged();
                }
            }, 2000);
        } else {
            // 加载的内容
            final ChatAdapter.RecyclerViewHolder recyclerViewHolder = (ChatAdapter.RecyclerViewHolder) holder;
            Community community = data.get(position);

            recyclerViewHolder.chatName.setText(community.getName());
            recyclerViewHolder.chatInfo.setText(community.getInfo());
            recyclerViewHolder.chatUser.setText(community.getOwner());

            recyclerViewHolder.itemView.setOnClickListener(click->{
                int i = recyclerViewHolder.getAdapterPosition();
                Intent intent = new Intent(context, Receive.class);
                intent.putExtra("id", data.get(i).getObjectId());
                intent.putExtra("chatName", data.get(i).getObjectId());
                intent.putExtra("chatInfo", community.getInfo());
                intent.putExtra("chatUser", community.getOwner());
            });

        }
    }

    @Override
    public int getItemCount() {
        if(data.size() < maxNum){
            return data.size();
        }
        return maxNum;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == (maxNum - 1)){
            return CommonConstant.F_TYPE;
        } else {
            return CommonConstant.N_TYPE;
        }
    }

    // 优化组件加载
    private class RecyclerViewHolder extends RecyclerView.ViewHolder {

        public TextView chatName;
        public TextView chatInfo;
        public TextView chatUser;
        public TextView loading;

        public RecyclerViewHolder(View itemView, int viewType) {
            super(itemView);

            if(viewType == CommonConstant.N_TYPE){
                chatName = itemView.findViewById(R.id.chat_fragment_name);
                chatInfo = itemView.findViewById(R.id.chat_fragment_info);
                chatUser = itemView.findViewById(R.id.chat_fragment_user);
            } else if(viewType == CommonConstant.F_TYPE){
                loading = itemView.findViewById(R.id.home_fragment_foot_item);
            }
        }
    }
}
